package org.sanmao.wyj.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.sanmao.wyj.controller.utils.Result;
import org.sanmao.wyj.domain.*;
import org.sanmao.wyj.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private WorkerService workerService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private AdvertisementService advertisementService;

    @Autowired
    private MessageService messageService;

    @Autowired
    private CashoutService cashoutService;

    @Autowired
    private ComplainService complainService;

//    登录拿token
    @PostMapping("/login")
    public Result login(@RequestBody Admin admin){
        String token = adminService.findAdminByUsernameAndPassword(admin);
        if(token != null){
            return Result.success(token);
        }
        else return Result.failure(-1,"账号或密码错误");
    }
//    查所有users
    @GetMapping("/users")
    public Result findAllUsers(){
        return  Result.success(userService.findAllUsers());
    }

    /**
     *分页查users
     * @param currentPage 当前页
     * @param pageSize 每页条数
     * @return
     */
    @GetMapping("/users/{currentPage}/{pageSize}")
    public Result findUsersPage(@PathVariable int currentPage,@PathVariable int pageSize){
        IPage<User> page = userService.findPage(currentPage, pageSize);
//    如果当前页码值大于最大页码值，那么把当前页码值替换成最大页码值
        if(currentPage>page.getPages())
        {
            page = userService.findPage((int) page.getPages(), pageSize);
        }
        return Result.success(page);
    }
//    根据id查uesr
    @GetMapping("/users/{id}")
    public Result findUserById(@PathVariable int id){
        return  Result.success(userService.findUserById(id));
    }
//    根据名字模糊查询user
    @GetMapping(value = "/users/{currentPage}/{pageSize}",params = {"name"})
    public Result findUserByName(@RequestParam String name,@PathVariable int currentPage,@PathVariable int pageSize){
        return Result.success(userService.findUserByName(name,currentPage,pageSize));
    }
//  注册user
    @PostMapping("/users")
    public Result saveUser(@RequestBody User user){
        return Result.success(userService.saveUser(user));
    }
//编辑user
    @PutMapping("/users")
    public Result updateUser(@RequestBody User user){
        return  Result.success(userService.updateUser(user));
    }
//根据id删除user
    @DeleteMapping("/users/{id}")
    public Result deleteUserById(@PathVariable int id){
        return  Result.success(userService.deleteUserById(id));
    }
//    查所有workers
    @GetMapping("/workers")
    public Result findAllWorkers(){
        return  Result.success(workerService.findAllWorkers());
    }

    /**
     *
     * @param currentPage 当前页
     * @param pageSize 每页条数
     * @return
     */
    @GetMapping("/workers/{currentPage}/{pageSize}")
    public Result findWorkersPage(@PathVariable int currentPage,@PathVariable int pageSize){
        IPage<Worker> page = workerService.findWorkersPage(currentPage, pageSize);
//    如果当前页码值大于最大页码值，那么把当前页码值替换成最大页码值
        if(currentPage>page.getPages())
        {
            page = workerService.findWorkersPage((int) page.getPages(), pageSize);
        }
        return Result.success(page);
    }
//根据id查worker
    @GetMapping("/workers/{id}")
    public Result findWorkerById(@PathVariable int id){
        return  Result.success(workerService.findWorkerById(id));
    }

//    根据名字模糊查询worker
    @GetMapping(value = "/workers/{currentPage}/{pageSize}",params = {"name"})
    public Result findWorkersByName(@RequestParam String name,@PathVariable int currentPage,@PathVariable int pageSize){
        return Result.success(workerService.findWorkersByName(name,currentPage,pageSize));
    }
//注册worker
    @PostMapping("/workers")
    public Result saveWorker(@RequestBody Worker worker){
        return Result.success(workerService.saveWorker(worker));
    }
//编辑worker
    @PutMapping("/workers")
    public Result updateWorker(@RequestBody Worker worker){
        return  Result.success(workerService.updateWorker(worker));
    }
//根据id删除worker
    @DeleteMapping("/workers/{id}")
    public Result deleteWorkerById(@PathVariable int id){
        return  Result.success(workerService.deleteWorkerById(id));
    }
//查所有orders
    @GetMapping("/orders")
    public Result findAllOrders(){
        return Result.success(orderService.findAllOrders());
    }
    /**
     *
     * @param currentPage 当前页
     * @param pageSize 每页条数
     * @return
     */
    @GetMapping("/orders/{currentPage}/{pageSize}")
    public Result findOrdersPage(@PathVariable int currentPage,@PathVariable int pageSize){
        IPage<Order> page = orderService.findOrdersPage(currentPage, pageSize);
//    如果当前页码值大于最大页码值，那么把当前页码值替换成最大页码值
        if(currentPage>page.getPages())
        {
            page = orderService.findOrdersPage((int) page.getPages(), pageSize);
        }
        return Result.success(page);
    }
//    根据oid查order
    @GetMapping("/orders/{id}")
    public Result findOrderById(@PathVariable int id){
        return Result.success(orderService.findFullOrderByOid(id));
    }
//根据内容模糊查询orders
    @GetMapping(value = "/orders/{currentPage}/{pageSize}",params = {"con"})
    public Result fuzzyFindOrders(@RequestParam String con,@PathVariable int currentPage,@PathVariable int pageSize){
        return Result.success(orderService.fuzzyFindOrders(con,currentPage,pageSize));
    }

//根据id删除order
    @DeleteMapping("/orders/{id}")
    public Result deleteOrderById(@PathVariable int id){
        return Result.success(orderService.deleteOrderByOid(id));
    }
//分页查ads
    @GetMapping("/ads/{currentPage}/{pageSize}")
    public Result findAllAds(@PathVariable int currentPage,@PathVariable int pageSize){
        IPage<Advertisement> page = new Page(currentPage,pageSize);
        return Result.success(advertisementService.page(page));
    }

    @GetMapping("/ads/{aid}")
    public Result findAd(@PathVariable int aid){
        return Result.success(advertisementService.getById(aid));
    }

//添加ad
    @PostMapping("/ads")
    public Result saveAd(@RequestBody Advertisement ad){
        if(advertisementService.addAd(ad)){
            return Result.success();
        }
        else return Result.failure(-1,"添加失败！");
    }

//    编辑ad
    @PutMapping("/ads")
    public Result updateAd(@RequestBody Advertisement ad){
        if(advertisementService.updateAd(ad)){
            return Result.success();
        }
        else return  Result.failure(-1,"编辑失败！");
    }

//    删除ad
    @DeleteMapping("/ads/{id}")
    public Result deleteAdById(@PathVariable int id){
        return  Result.success(advertisementService.removeById(id));
    }


//    查看留言
    @GetMapping("/msgs/{currentPage}/{pageSize}")
    public Result findAllMsgs(@PathVariable int currentPage,@PathVariable int pageSize){
        IPage<Message> page = new Page<>(currentPage,pageSize);
        return Result.success(messageService.page(page));
    }

//    删除留言
    @DeleteMapping("/msgs/{id}")
    public Result delMsgById(@PathVariable int id){
        return Result.success(messageService.removeById(id));
    }

//    查单提现记录
    @GetMapping("/cashout/{currentPage}/{pageSize}")
    public Result findCashoutList(@PathVariable int currentPage,@PathVariable int pageSize,@RequestParam int state){
        return Result.success(cashoutService.findCashoutList(currentPage,pageSize,state));
    }

//    处理提现
    @PutMapping("/cashout/{id}")
    public Result doCashoutByid(@PathVariable int id){
        if(cashoutService.doCashoutByid(id)){
            return   Result.success();
        }
        return Result.failure(-1,"请求失败，请重试或联系管理员！");
    }

//  查看投诉列表
    @GetMapping("/orders/complain/{currentPage}/{pageSize}")
    public Result findComplain(@PathVariable int currentPage,@PathVariable int pageSize){
        return Result.success(complainService.findComplainList(currentPage,pageSize));
    }

}
